package com.github.satant.job.common.rest;

import com.github.satant.job.common.biz.BaseBiz;
import com.github.satant.job.common.msg.ObjectRestResponse;
import com.github.satant.job.common.msg.TableResultResponse;
import com.github.satant.job.common.util.Query;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author: dear_satant
 * @description:
 * @date: 2018/6/24 20:43
 */
public class BaseController<Service extends BaseBiz,Entity> {
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected Service baseService;

    @RequestMapping(value = "",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增对象")
    public ObjectRestResponse<Object> add(@RequestBody Entity entity){
      Object o = baseService.insertSelective(entity);
      return new ObjectRestResponse<Object>().data(o);
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "通过id获取单个对象")
    public ObjectRestResponse<Entity> get(@PathVariable int id){
        ObjectRestResponse<Entity> entityObjectRestResponse = new ObjectRestResponse<>();
        Object o = baseService.selectById(id);
        entityObjectRestResponse.data((Entity)o);
        return entityObjectRestResponse;
    }

    @RequestMapping(value = "",method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value = "修改对象，必须传ID")
    public ObjectRestResponse<Entity> update(@RequestBody Entity entity){
        baseService.updateSelectiveById(entity);
        return new ObjectRestResponse<Entity>();
    }
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation(value = "根据ID删除对象")
    public ObjectRestResponse<Entity> remove(@PathVariable int id){
        baseService.deleteById(id);
        return new ObjectRestResponse<Entity>();
    }

    @RequestMapping(value = "/all",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询所有对象")
    public List<Entity> all(){
        return baseService.selectListAll();
    }

    @RequestMapping(value = "/page",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "不包含排序的分页查询对象,需要传递page(当前页),limit(每页显示记录数)，"
        + "还可以传递javabean中的一个或多个所有属性，进行模糊查询。")
    public TableResultResponse<Entity> list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        return baseService.selectByQuery(query);
    }


    @RequestMapping(value = "/page/{orderField}/{orderType}",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "包含排序的分页查询对象,需要传递page(当前页,不传默认为1),limit(每页显示记录数，不传默认为20)，"
        + "还可以传递javabean中的一个或多个所有属性，进行模糊查询。"
        + "URL中，page之后要传递的是分别是排序字段和排序方式。"
        + "如果有多个排序字段，按英文','分隔，排序方式同理。排序字段和排序方式个数应相同。")
    public TableResultResponse<Entity> listAndOrder(
        @ApiParam(value = "示例url：http://localhost:8601/admin/page/createTime,id/a,d?page=1&limit=20&name=satant。"
            +"此URL的含义解读：在admin中查询名字包含satant的用户,根据创建时间升序再按照id降序排列，取第一条到第20条数据")
            @RequestParam Map<String, Object> params,
        @ApiParam("排序字段，示例createTime,id。不需要排序，传递0")@PathVariable("orderField") String orderField,
        @ApiParam("排序字段d(降序);a(升序)，示例d,a。不需要排序传递0")@PathVariable("orderType") String orderType){
        //查询列表数据
        Query query = new Query(params);
        return baseService.selectByQueryWithOrder(query,orderField,orderType);
    }
}
